//
// Created by Bili_TianX_Computer on 2021/7/10.
//

#include "AddProductDialog.h"

AddProductDialog::AddProductDialog()
{
    box = new QDialogButtonBox(QDialogButtonBox::Ok | QDialogButtonBox::Cancel);
    nameEdit = new QLineEdit();
    purchasePriceEdit = new QLineEdit();
    sellingPriceEdit = new QLineEdit();
    stockEdit = new QLineEdit();
    unitEdit = new QLineEdit();
    editLayout = new QFormLayout();
    widgetLayout = new QVBoxLayout();

    editLayout->addRow("商品名称", nameEdit);
    editLayout->addRow("进价(元)", purchasePriceEdit);
    editLayout->addRow("售价(元)", sellingPriceEdit);
    editLayout->addRow("库存", stockEdit);
    editLayout->addRow("单位", unitEdit);
    widgetLayout->addLayout(editLayout);
    widgetLayout->addWidget(box);

    setWindowTitle("添加商品");
    setLayout(widgetLayout);

    box->button(QDialogButtonBox::Ok)->setText("确定");
    box->button(QDialogButtonBox::Cancel)->setText("取消");

    connect(box, SIGNAL(accepted()), this, SLOT(acceptedSlot()));
    connect(box, SIGNAL(rejected()), this, SLOT(rejectedSlot()));
}

void AddProductDialog::acceptedSlot()
{
    bool ok1, ok2, ok3;
    double purchasePrice = purchasePriceEdit->text().toDouble(&ok1);
    if (!ok1) QMessageBox::critical(this, "错误", "进价有误！");
    double sellingPrice = sellingPriceEdit->text().toDouble(&ok2);
    if (!ok2) QMessageBox::critical(this, "错误", "售价有误！");
    ushort stock = stockEdit->text().toUShort(&ok3);
    if (!ok3) QMessageBox::critical(this, "错误", "库存有误！");
    if (!(ok1 && ok2 && ok3)) return;
    product = new Product(nameEdit->text(), purchasePrice, sellingPrice, stock, unitEdit->text());
    this->accept();
}

void AddProductDialog::rejectedSlot()
{
    this->reject();
}


